package cn.topevery.garden.wapi.controller;

import cn.topevery.garden.dal.logic.PersonAttendanceLogic;
import cn.topevery.garden.entity.dto.PersonAttendanceDTO;
import cn.topevery.garden.entity.query.PersonAttendanceQuery;
import cn.topevery.garden.entity.vo.PersonAttendanceVO;
import cn.topevery.garden.entity.po.PersonAttendance;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.util.ExcelUtil;
import hw.topevery.framework.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.github.dozermapper.core.Mapper;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 考勤记录
 */
@Api(tags = "考勤记录")
@RestController
@RequestMapping("/personAttendance")
public class PersonAttendanceController extends BaseController {

    @Autowired
    private Mapper mapper;

    @Autowired
    PersonAttendanceLogic personAttendanceLogic;


    /**
     * 获取考勤记录分页列表
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "获取考勤记录页列表")
    @PostMapping("/listPage")
    public JsonResult<DataPager<PersonAttendanceVO>> listPage(@RequestBody PersonAttendanceQuery query) {
        return JsonResult.ok(personAttendanceLogic.listPage(query));
    }

    @ApiOperation("新增/编辑 考勤记录信息")
    @PostMapping("/saveOrUpdate")
    public JsonResult<Boolean> saveOrUpdate(@Valid @RequestBody PersonAttendanceDTO dto) {
        personAttendanceLogic.saveOrUpdate(getUser().id.toString(),mapper.map(dto, PersonAttendance.class));
        return JsonResult.ok(true);
    }

    @ApiOperation("通过Id获取详情")
    @GetMapping("/getInfoById")
    public JsonResult<PersonAttendanceVO> getInfoById(@RequestParam Integer id) {
        PersonAttendance entity = personAttendanceLogic.getEntity(id);
        PersonAttendanceVO vo= mapper.map(entity, PersonAttendanceVO.class);
        return JsonResult.ok(vo);
    }

    @ApiOperation("根据自增ID批量删除")
    @PostMapping("/deleteBatchById")
    public JsonResult<Boolean> deleteBatchById(@RequestBody Integer[] ids) {
        personAttendanceLogic.deleteBatchById(getUser().id.toString(),ids);
        return JsonResult.ok(true);
    }

    @ApiOperation("导出考勤记录信息")
    @GetMapping("/export")
    public void export(PersonAttendanceQuery query, HttpServletResponse response) {
        List<PersonAttendanceVO> list = personAttendanceLogic.listPage(query).records;
        ExcelUtil.exportExcel(list, "考勤记录", "sheet1", PersonAttendanceVO.class, "考勤记录", response);
    }

    @ApiOperation("人员综合分析")
    @PostMapping("/personAttendanceStat")
    public JsonResult personAttendanceStat(@RequestBody PersonAttendanceQuery query){
        DataPager<PersonAttendanceVO> personAttendanceVODataPager = personAttendanceLogic.personAttendanceStat(query);
        return JsonResult.ok(personAttendanceVODataPager);
    }
}
